package stormIO;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
 xmlSet ./Tests/arch/naeem-0.xml  /tmp/test.xml  1  WCET  1
 * 
 * @author richard
 *
 */
public class XmlFactory {

	private String filename;
	private String res="";
	private String attribut;
	private String id;
	private String value;
	boolean result=true;
	boolean nodeOK = false;
	Node Ref = null;
	String Refres ="";
	public XmlFactory(String filename) {
		this.filename = filename;
		
	   }
	public boolean proceed (String filenamed, String id, String attribut, String value) throws ParserConfigurationException, SAXException, IOException{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 		DocumentBuilder builder = dbf.newDocumentBuilder();
 	    Document document = builder.parse(filename);
 	   this.attribut = attribut;
	    this.id = id;
	    this.value = value;
	    
 	    modify( (Node)document);
	    if (!result ) return false;
	    
	    PrintWriter outputStream = new PrintWriter(new FileWriter(filenamed));
	    outputStream.print(res);
	    outputStream.close();
	    return true;
	    //System.out.println  ("proceed de XmlFactory ....  \n"+  this.res);
	    
	}
	
	
	/*
	 * xs /Users/richard/Documents/workspace/STORM/Tests/arch/romeo-0.xml  /tmp/exp0.xml   1 period 50
	 * 
	 */
	private void modify(Node document) {
		document.normalize();
		 if ((document.getNodeType() == org.w3c.dom.Node.TEXT_NODE) ){
			 
		 }
				 
			if(	 (document.getNodeType() == org.w3c.dom.Node.COMMENT_NODE)){
			 this.res = this.res+"<!--  " + document.getNodeValue()+ "--> \n";
		 }
		 if (document.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE  )  {
			 	this.res = this.res + "<" + document.getNodeName() ;
				Refres = res;
							 	
			 	
			 	//PArcours attributs 
			 	 NamedNodeMap Attrs = document.getAttributes();
			 	nodeOK = false;
			 	 for (int i =0 ; i < Attrs.getLength(); i++){
			 		 Node attr = Attrs.item(i);
			 	 if ( (attr.getNodeName()).equalsIgnoreCase("id") ) {
			 			if ( (attr.getNodeValue()).equalsIgnoreCase(id) ) {
			 				if (!nodeOK) {
			 					nodeOK = true;
			 					res = Refres; 
			 					i=-1;
			 					continue;
			 				}
			 				
			 			
			 			}
			 		 }
			 	//	 if ((nodeOK) ) System.out.println("OK2  " + attr.getNodeName() + "  "  + this.attribut );
			 		 if ((nodeOK) && (attr.getNodeName()).equalsIgnoreCase(attribut)) {
			 			 
			 			 this.res = this.res + " " +  attr.getNodeName() + " " ;
			 			 this.res = this.res + "=\"" +  this.value + "\" " ;
			 			 this.result = true;
			 		 }
			 		 else {
			 			 this.res = this.res + " " +  attr.getNodeName() + " " ;
			 			 this.res = this.res + "=\"" +  attr.getNodeValue() + "\" " ;
			 		 }
			 	 }
			 	
				this.res = this.res + ">\n" ;
 	
			 	
			 	
		 }

		NodeList listeNode = document.getChildNodes();
		for (int i =0 ; i < listeNode.getLength(); i ++ ) {
			modify (listeNode.item(i));
		}
		 if (document.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE  )  {
			 	this.res = this.res + "</" + document.getNodeName()  + "> \n"; 
		 }
		// TODO Auto-generated method stub
		
	}
		
}


